{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Anaconda 默认不包含 docx工具包，需要执行以下脚本进行安装\n",
    "# 如果本地没有安装工具包，请去掉“#”，再执行\n",
    "# !pip install -i https://mirrors.aliyun.com/pypi/simple/ python-docx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、导入包并创建一个 Word 文档对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从docx 工具包中导入要使用的对象\n",
    "from docx import Document\n",
    "# 从docx 工具包中导入要使用的对象\n",
    "from docx_ext import add_hyperlink, add_bookmark\n",
    "\n",
    "# Document 不包含文件路径就是在内存中创建 Word 对象\n",
    "document = Document()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、增加一个书签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_bookmark = document.add_paragraph()\n",
    "# 创建一个书签，等会通过链接\n",
    "add_bookmark(paragraph=p_bookmark, text='这是一个书签', name='回到这里')\n",
    "\n",
    "save_file = r'.\\files\\Word_书签链接.docx'\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3、增加一个网页链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "document.add_heading('一个超链接', level=1)\n",
    "# 在一个段落中增加链接\n",
    "p_lnk_1 = document.add_paragraph()\n",
    "add_hyperlink(paragraph=p_lnk_1,          # 段落对象\n",
    "              url='http://www.baidu.com', # 链接地址\n",
    "              text='百度',                # 链接文本\n",
    "              tooltip=None,               # 链接提示（为空则为 url）\n",
    "              font=\"微软雅黑\",            # 字体\n",
    "              color=None,                 # 颜色\n",
    "              underline=True)             # 下划线\n",
    "\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4、同一段落增加多个网页链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "document.add_heading('多个超链接', level=1)\n",
    "p_lnk_2 = document.add_paragraph()\n",
    "add_hyperlink(paragraph=p_lnk_2,\n",
    "              url='https://www.163.com',\n",
    "              text='网易-163',\n",
    "              tooltip=\"网易：www.163.com\",\n",
    "              font=\"微软雅黑\",\n",
    "              color=\"FF0000\", # RGB：红色\n",
    "              underline=False)\n",
    "p_lnk_2.add_run(' | ') # 加一个分隔符文字块\n",
    "\n",
    "add_hyperlink(paragraph=p_lnk_2,\n",
    "              url='https://www.126.com',\n",
    "              text='网易-126',\n",
    "              tooltip=\"网易：www.126.com\",\n",
    "              font=\"微软雅黑\",\n",
    "              color=\"00FF00\", # RGB：绿色\n",
    "              underline=False)\n",
    "p_lnk_2.add_run(' | ') # 加一个分隔符文字块\n",
    "\n",
    "add_hyperlink(paragraph=p_lnk_2,\n",
    "              url='https://www.yeah.net',\n",
    "              text='网易-yeah',\n",
    "              tooltip=\"网易：www.yeah.net\",\n",
    "              font=\"微软雅黑\",\n",
    "              color=\"0000FF\", # RGB：蓝色\n",
    "              underline=False)\n",
    "\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题\n",
    "\n",
    "\n",
    "* 打开文件 docx_ext.py，可以看到一些 OxmlElement('w:r') 的代码，我们将会在“Word超级玩法”中进行讲解。\n",
    "\n",
    "```python\n",
    "from docx_ext import add_hyperlink, add_bookmark\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
